package com.next

import grails.converters.JSON
import org.springframework.security.access.annotation.Secured

import java.text.SimpleDateFormat

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
class SalesDashboardController
{
    def contactService
    def opportunityService
    def springSecurityService

    def index()
    {
    }

    def visualization()
    {
        def username = springSecurityService?.getPrincipal()?.username
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM")
        Calendar currentDay = Calendar.getInstance()
        Calendar currentWeek = Calendar.getInstance()
        Calendar currentMonth = Calendar.getInstance()
        Calendar currentSeason = Calendar.getInstance()
        Calendar currentYear = Calendar.getInstance()
        Calendar endWeek = Calendar.getInstance()
        Calendar endMonth = Calendar.getInstance()
        Calendar endSeason = Calendar.getInstance()
        Calendar endYear = Calendar.getInstance()
        int season = Math.floor((currentSeason.get(Calendar.MONTH)) / 3)
        if (currentWeek.get(Calendar.DAY_OF_WEEK) == 1)
        {
            currentWeek.add(Calendar.DATE, -6)
        }
        else
        {
            currentWeek.set(Calendar.DAY_OF_WEEK, 2)
        }

        currentMonth.set(Calendar.DAY_OF_MONTH, 1)
        currentSeason.set(Calendar.DAY_OF_MONTH, 1)
        currentSeason.set(Calendar.MONTH, 3 * season)
        currentYear.set(Calendar.DAY_OF_YEAR, 1)
        endWeek.set(Calendar.DAY_OF_WEEK, 1)
        endWeek.add(Calendar.DATE, 7)
        endMonth.set(Calendar.DAY_OF_MONTH, 1)
        endMonth.add(Calendar.MONTH, 1)
        endSeason.set(Calendar.DAY_OF_MONTH, 1)
        if (season == 4)
        {
            endSeason.set(Calendar.MONTH, 0)
            endSeason.add(Calendar.YEAR, 1)
        }
        else
        {
            endSeason.set(Calendar.MONTH, 3 * season)
        }
        endYear.set(Calendar.DAY_OF_YEAR, 1)
        endYear.add(Calendar.YEAR, 1)
        String cDay = sdf.format(currentDay.getTime())
        String cWeek = sdf.format(currentWeek.getTime())
        String cMonth = sdf.format(currentMonth.getTime())
        String cSeason = sdf.format(currentSeason.getTime())
        String cYear = sdf.format(currentYear.getTime())
        String eWeek = sdf.format(endWeek.getTime())
        String eMonth = sdf.format(endMonth.getTime())
        String eSeason = sdf.format(endSeason.getTime())
        String eYear = sdf.format(endYear.getTime())
        def amount = []
        def amount2 = []
        for (
            def i = 0;
                i < 20;
                i++)
        {
            amount[i] = 0
            amount2[i] = 0
        }

        def opportunityList = Opportunity.findAll("from Opportunity where createdDate >= to_date('${cMonth}','YYYY-MM-DD HH24:MI:SS') and user.username = '${username}'")

        def activityList = Activity.findAll("from Activity where startTime >= to_date('${cYear}','YYYY-MM-DD HH24:MI:SS') and user.username = '${username}'")

        activityList.each {
            if (it.type.name == 'Appointment')
            {
                if (it.startTime.after(currentDay.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[0]++
                    }
                    else
                    {
                        amount[1]++
                    }
                }
                if (it.startTime.after(currentWeek.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[2]++
                    }
                    else
                    {
                        amount[3]++
                    }
                }
                if (it.startTime.after(currentMonth.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[4]++
                    }
                    else
                    {
                        amount[5]++
                    }
                }
                if (it.startTime.after(currentSeason.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[6]++
                    }
                    else
                    {
                        amount[7]++
                    }
                }
                if (it.startTime.after(currentYear.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[8]++
                    }
                    else
                    {
                        amount[9]++
                    }
                }
            }
            if (it.type.name == 'Task')
            {
                if (it.startTime.after(currentDay.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[10]++
                    }
                    else
                    {
                        amount[11]++
                    }
                }
                if (it.startTime.after(currentWeek.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[12]++
                    }
                    else
                    {
                        amount[13]++
                    }
                }
                if (it.startTime.after(currentMonth.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[14]++
                    }
                    else
                    {
                        amount[15]++
                    }
                }
                if (it.startTime.after(currentSeason.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[16]++
                    }
                    else
                    {
                        amount[17]++
                    }
                }
                if (it.startTime.after(currentYear.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[18]++
                    }
                    else
                    {
                        amount[19]++
                    }
                }
            }
        }

        opportunityList.each {
            if (it.stage?.name == '已入围')
            {
                if (it.status?.name == '跟进中')
                {
                    amount2[0] += it.amount
                }
                if (it.status?.name == '已成功')
                {
                    amount2[1] += it.amount
                }
                if (it.status?.name == '已失败')
                {
                    amount2[2] += it.amount
                }
            }
            if (it.stage?.name == '需求分析')
            {
                if (it.status?.name == '跟进中')
                {
                    amount2[3] += it.amount
                }
                if (it.status?.name == '已成功')
                {
                    amount2[4] += it.amount
                }
                if (it.status?.name == '已失败')
                {
                    amount2[5] += it.amount
                }
            }
            if (it.stage?.name == '解决方案')
            {
                if (it.status?.name == '跟进中')
                {
                    amount2[6] += it.amount
                }
                if (it.status?.name == '已成功')
                {
                    amount2[7] += it.amount
                }
                if (it.status?.name == '已失败')
                {
                    amount2[8] += it.amount
                }
            }
            if (it.stage?.name == '谈判')
            {
                if (it.status?.name == '跟进中')
                {
                    amount2[9] += it.amount
                }
                if (it.status?.name == '已成功')
                {
                    amount2[10] += it.amount
                }
                if (it.status?.name == '已失败')
                {
                    amount2[11] += it.amount
                }
            }
            if (it.stage?.name == '接近成功')
            {
                if (it.status?.name == '跟进中')
                {
                    amount2[12] += it.amount
                }
                if (it.status?.name == '已成功')
                {
                    amount2[13] += it.amount
                }
                if (it.status?.name == '已失败')
                {
                    amount2[14] += it.amount
                }
            }
        }
        render([amount: amount, amount2: amount2] as JSON)
    }
}
